package com.enniswu.weitter.user.userlist;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BaseActivity;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.widget.adapter.UserAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class UserListActivity extends BaseActivity<UserListContract.Presenter> implements UserListContract.View {

    public static final int TITLE_FOLLOWING = 0;
    public static final int TITLE_FOLLOWS = 1;

    @BindView(R.id.rv_user)
    RecyclerView rvUser;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    private List<User> users;
    private UserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);
        List<Integer> ids = (List<Integer>) getIntent().getSerializableExtra("ids");
        if (getIntent().getIntExtra("title", 0) == TITLE_FOLLOWS)
            setTitle(R.string.title_follows);
        else
            setTitle(R.string.title_following);
        if (ids == null || ids.isEmpty()) {
            return;
        }

        tvEmpty.setVisibility(View.GONE);
        users = new ArrayList<>();
        adapter = new UserAdapter(users);
        rvUser.setLayoutManager(new LinearLayoutManager(this));
        rvUser.setAdapter(adapter);
        new UserListPresenter(this);
        presenter.getUser(ids);
    }

    @Override
    public void addUser(User user) {
        users.add(user);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemInserted(users.size());
                adapter.notifyItemRangeChanged(users.size(), 1);
            }
        });
    }
}
